package org.example.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(3307);
        System.out.println("服务器启动，等待连接");
        Socket accept = server.accept();
        System.out.println("连接成功");
        InputStream input = accept.getInputStream();
        OutputStream out = accept.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        byte[] b = new byte[1024];
        while (true) {
            int read = input.read(b);
            if (read > 0) {
                System.out.println("接收到客户端消息：" + new String(b, 0, read));
            }
            System.out.println("输入传输给客户端的信息：");
            Scanner sc = new Scanner(System.in);
            String message = sc.nextLine();
            if (message.equals("quit")){
                break;
            }
            bw.write(message);
            bw.flush();
        }
        bw.close();
        out.close();
        input.close();

    }
}
